<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('线路配置列表')" />
    <style>
        /* 模态框样式 */
        .modal-content {
            width: 80%;
            margin: 15% auto;
        }
    </style>
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>网关名称：</label>
                                <input type="text" name="gwName"/>
                            </li>
                            <li>
                                <label>profile：</label>
                                <select name="profileName">
                                    <option value="">所有</option>
                                    <option th:each="profileData:${profileList}" th:value="${profileData.profileName}" th:text="${profileData.profileName}" ></option>
                                </select>
                            </li>
                            <li>
                                <label>主叫号码：</label>
                                <input type="text" name="caller"/>
                            </li>
                            <li>
                                <label>被叫前缀：</label>
                                <input type="text" name="calleePrefix"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="cc:gateways:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="cc:gateways:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-info" onclick="createJson()" >
                    <i class="fa fa-search"></i> 生成网关配置json
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>

            <!-- 模态框 -->
            <div id="myModal" class="modal fade" tabindex="-1" role="dialog">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title">JSON 数据</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <pre id="jsonOutput"></pre>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>

    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('cc:gateways:edit')}]];
        var prefix = ctx + "cc/gateways";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                modalName: "线路配置",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '${comment}',
                    visible: false
                },
                {
                    field: 'gwName',
                    title: '网关名称'
                },
                {
                    field: 'profileName',
                    title: 'profile名称'
                },
                {
                    field: 'caller',
                    title: '主叫号码'
                },
                {
                    field: 'calleePrefix',
                    title: '被叫前缀'
                },
                {
                    field: 'gwAddr',
                    title: '网关地址'
                },
                {
                    field: 'codec',
                    title: '外呼语音编码'
                },
                {
                    field: 'gwName',
                    title: '网关名称描述'
                },
                {
                    field: 'maxConcurrency',
                    title: '网关最大并发数'
                },
                {
                    field: 'authUsername',
                    title: '认证用户名'
                },
                {
                    field: 'authPassword',
                    title: '认证密码'
                },
                {
                    field: 'priority',
                    title: '使用优先级'
                },
                {
                    field: 'register',
                    title: '是否认证注册',
                    formatter: function(value, item, index) {
                        if (item.register == '0') {
                            return '<span class="badge badge-primary">对接模式</span>';
                        }
                        else if (item.register == '1') {
                            return '<span class="badge badge-success">注册模式</span>';
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        function createJson(){

            // 获取 Bootstrap Table 数据
            const tableData = $('#bootstrap-table').bootstrapTable('getSelections');
            if ($.common.isEmpty(tableData)) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            console.log(tableData);

            const jsonData = tableData.map((item, index) => ({
                uuid: String(item.id), // 生成 uuid，从 "01" 开始
                updateTime: item.updateTime,
                gatewayAddr: item.gwAddr,
                callerNumber: item.caller,
                calleePrefix: item.calleePrefix,
                priority: item.priority,
                concurrency: item.maxConcurrency,
                register: Boolean(item.register), // 将数字转换为布尔值
                audioCodec: item.codec
            }));

            // 将数据转换为 JSON 字符串
            const jsonString = JSON.stringify(jsonData, null, 4); // 使用缩进美化 JSON 字符串
            console.log(jsonString);

            // 显示模态框并输出 JSON 字符串
            $('#jsonOutput').text(jsonString);
            $('#myModal').modal('show');
        }

    </script>
</body>
</html>